package com.wyu.blog.config;

import com.wyu.blog.component.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by hgq on 2020/01/10.
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Bean
    public HandlerInterceptor getArticleClickInterceptor(){
        return new ArticleClickInterceptor();
    }

    @Bean
    public HandlerInterceptor getSessionInterceptor(){
        return new SessionInterceptor();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/main").setViewName("main");
        registry.addViewController("/admin").setViewName("admin/login");
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").
//                excludePathPatterns("/","/user/register","/blog","/sign_in",
//                        "/sign_up","/article/detail/**","/main","/search/**");
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/write","/person/**","/settings",
                "/collect/**","/fans/**","/concern/**","/article/category/**","/article/update");
        registry.addInterceptor(new AdminHandlerInterceptor()).addPathPatterns("/manager/**");
        registry.addInterceptor(getArticleClickInterceptor()).addPathPatterns("/article/detail/**");
        registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/img/**").addResourceLocations("file:D:/upload/");
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

}
